<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户编号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户渠道">
        <el-select v-model="queryParams.customerFromId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_from"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="customerContact">
        <el-input
          v-model="queryParams.customerContact"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="customerCompany">
        <el-input
          v-model="queryParams.customerCompany"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型">
        <el-select v-model="queryParams.customerTypeId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_type"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户状态">
        <el-select v-model="queryParams.customerStatusId"
                   placeholder="请选择"
                   size='mini'>
          <el-option v-for="dict in dict.type.ipms_customer_status"
                     :key="dict.value"
                     :label="dict.label"
                     :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="customerNo" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
          >{{scope.row.customerNo}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="客户渠道" align="center" prop="customerFrom"  :formatter="formatFrom" />
      <el-table-column label="联系人" align="center" prop="customerContact" />
      <el-table-column label="联系人电话" align="center" prop="customerPhone" />
      <el-table-column label="联系人邮箱" align="center" prop="customerEmail" />
      <el-table-column label="公司名称" align="center" prop="customerCompany" />
      <el-table-column label="客户单位类型" align="center" prop="customerTypeId" :formatter="formatType"/>
      <el-table-column label="公司地址" align="center" prop="customerAddress" />
      <el-table-column label="客户业务次数" align="center" prop="customerExperience" />
      <el-table-column label="客户状态" align="center" prop="customerStatusId" :formatter="formatStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCon(scope.row)"
          >合同</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-return"
            @click="handleReturn(scope.row)"
          >回访</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="客户编号" prop="customerNo">
          <el-input v-model="form.customerNo" placeholder="系统生成" disabled/>
        </el-form-item>
        <el-form-item label="客户渠道">
          <el-select v-model="form.customerFromId"
                     placeholder="请选择"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_from"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="customerContact">
          <el-input v-model="form.customerContact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="customerEmail">
          <el-input v-model="form.customerEmail" placeholder="请输入联系人邮箱" />
        </el-form-item>
        <el-form-item label="公司名称" prop="customerCompany">
          <el-input v-model="form.customerCompany" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="单位类型">
          <el-select v-model="form.customerTypeId"
                     placeholder="请选择单位类型"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_type"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="业务经历" prop="customerExperience">
          <el-input v-model="form.customerExperience" placeholder="系统维护" disabled />
        </el-form-item>
        <el-form-item label="客户状态">
          <el-select v-model="form.customerStatusId"
                     placeholder="请选择"
                     size='mini'>
            <el-option v-for="dict in dict.type.ipms_customer_status"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加客户回访对话框 -->
    <el-dialog :title="title" :visible.sync="openTwo" width="800px" append-to-body>
      <el-form ref="TrackingForm" :model="TrackingForm" :rules="rules" label-width="90px">

        <el-form-item label="回复信息" prop="customerExperience" >
          <el-input  v-model="TrackingForm.trackingMsg" placeholder="请输入回访信息"  />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTrackingForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/crm/crm";
import {addTracking} from "../../../api/crm/crm";
import {sendEmail} from "../../../api/patent/ican";

export default {
  dicts:["ipms_customer_from","ipms_customer_type","ipms_customer_status"],
  name: "Customer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openTwo:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId:null,
        customerNo: null,
        customerFromId: null,
        customerContact: null,
        customerPhone: null,
        customerEmail: null,
        customerCompany: null,
        customerTypeId: null,
        customerAddress: null,
        userId: null,
        customerExperience: null,
        customerStatusId:null,
        version: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      },
      trackingForm:{
        trackingId: null,
        customerId:null,
        trackingMsg:null,
        version: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      },
      // 表单参数
      form: {},
      //绑定回访表单
      TrackingForm:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //格式化字典参数
    formatFrom(row){
      return this.selectDictLabel(this.dict.type.ipms_customer_from,row.customerFromId)
    },
    formatType(row){
      return this.selectDictLabel(this.dict.type.ipms_customer_type,row.customerTypeId)
    },
    formatStatus(row){
      return this.selectDictLabel(this.dict.type.ipms_customer_status,row.customerStatusId)
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openTwo=false;
    },
    // 表单重置
    reset() {
      this.form = {
        customerId: null,
        customerNo: null,
        customerFromId: null,
        customerContact: null,
        customerPhone: null,
        customerEmail: null,
        customerCompany: null,
        customerTypeId: null,
        customerAddress: null,
        userId: null,
        customerExperience: null,
        customerStatusId: null,
        version: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      };
      this.TrackingForm={
        trackingId: null,
        customerId:null,
        trackingMsg:null,
        Eamil:null,
        version: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerId = row.customerId || this.ids
      getCustomer(customerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 客户表单提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerId != null) {
            console.log(this.form);
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 回访表单提交按钮 */
    submitTrackingForm(row){
        addTracking(this.TrackingForm).then(response => {
          // this.$modal.msgSuccess("新增成功");
          this.openTwo = false;
        });

       console.log(this.TrackingForm.Eamil);

        sendEmail(this.TrackingForm.Eamil,this.TrackingForm.trackingMsg).then(response =>{
          this.$modal.msgSuccess("发送回访邮件成功");
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerIds = row.customerId || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + customerIds + '"的数据项？').then(function() {
        return delCustomer(customerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    },
    /** 跳转客户详情 */
    handleDetails(row){
      const customerId = row.customerId || this.ids
      getCustomer(customerId).then(response => {
        this.$router.push({
          path:'/crm/details',
          query:{
            data:response.data
          }
        })
      });
    },
    //进入合同页面
    handleCon(row){
      const customerId = row.customerId || this.ids
       this.$store.commit("SET_CUSTOMER_ID", customerId);
        this.$router.push({
          path:'/contract/add',
          // query:{
          //   customerId:customerId
          // }
        })
    },
    //新增客户回访
    handleReturn(row){
      this.reset();
      this.TrackingForm.customerId=row.customerId;
      this.TrackingForm.Eamil=row.customerEmail;
      this.openTwo = true;
      this.title = "新增回访记录";
    },
  }
};
</script>

